﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Text;

namespace CmcWeb
{
    public static class HelpPager
    {
        /// <summary>
        ///   分页pager显示
        /// </summary>
        /// <param name="html"></param>
        /// <param name="currentPageStr">没用加的好玩</param>
        /// <param name="pageSize">每页显示</param>
        /// <param name="totalCount">总数据量</param>
        /// <returns></returns>
        public static MvcHtmlString pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
        {
            var stringPage = html.ViewContext.HttpContext.Request.QueryString["page"];//获取当前url中page的值
            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            int currentPage = 1;
            var totalPages = Math.Max((totalCount + pageSize - 1)/pageSize, 1);//总页数
            //与相应的QueryString绑定
            if (!string.IsNullOrEmpty(stringPage))
            {
                currentPage = int.Parse(stringPage);//当前页
            }

            var dict = new UrlHelper(html.ViewContext.RequestContext);
            var que = new RouteValueDictionary();
            var output = new StringBuilder();

            for (int i = 0; i < queryString.Count; i++)
            {
                que.Add(queryString.Keys[i], queryString[i].ToString());
            }

            if (totalPages > 1)
            {
                if (currentPage != 1)
                {
                    //处理首页链接
                    que["page"] = string.Empty;
                    output.AppendFormat("<a href=\"{0}\" title=\"第一页\" class=\"page-start\">首页</a>",
                                    dict.RouteUrl(que));
                }

                if (currentPage > 1)
                {
                    //处理上一页
                    que["page"] = String.Format("{0}", currentPage - 1);
                    output.AppendFormat("<a href=\"{0}\" title=\"上一页\" class=\"page-prev\">上一页</a>",
                                    dict.RouteUrl(que));
                }

　　　　　　//可以自己随便写，给的例子
                for (int i = 0; i < 5; i++)
                {
                    //当前页
                    que["page"] = String.Format("{0}", currentPage + i);
                    output.AppendFormat("<a href=\"{1}\"  class=\"page-cur\" title=\"第{0}页\">{0}</a>", currentPage + i, dict.RouteUrl(que));    
                }

                if (currentPage < totalPages)
                {
                    //处理下一页的链接
                    que["page"] = String.Format("{0}", currentPage + 1);
                    output.AppendFormat("<a href=\"{0}\" title=\"下一页\" class=\"page-prev\">下一页</a>",
                                    dict.RouteUrl(que));
                }

                if (currentPage != totalPages)
                {
                    que["page"] = String.Format("{0}", totalPages);
                    output.AppendFormat("<a href=\"{0}\" title=\"末页\" class=\"page-prev\">末页</a>",
                                    dict.RouteUrl(que));
                }
            }
            return MvcHtmlString.Create(output.ToString());
        }
    }
}
    